<?

class Author_model extends Model {

	public function __construct() {

		parent::Model();

	}

	public function get_login_validation_rules() {
		$rules['username'] = 'required|min_length[6]|max_length[20]|alpha_dash';
		$rules['password'] = 'required|min_length[6]|alpha_dash';
		return $rules;
	}

	public function get_login_validation_fields() {
		$fields['username'] = "Username";
		$fields['password'] = "Password";
		return $fields;
	}

	public function get_create_validation_rules() {

		$rules['username'] = 'required|min_length[6]|max_length[20]|alpha_dash';
		$rules['password'] = 'required|min_length[6]|alpha_dash|matches[confirm_password]';
		$rules['confirm_password'] = 'required';
		$rules['email'] = 'valid_email';
		return $rules;

	}

	public function get_create_validation_fields() {
		$fields['username'] = "Username";
		$fields['password'] = "Password";
		$fields['confirm_password'] = 'Confirmation password';
		$fields['email'] = "Email address";
		return $fields;
	}

	public function insert_author() {
		$crypt_pass = crypt($this->input->post('password',TRUE));
		$username = $this->input->post('username',TRUE);
		$email = $this->input->post('email',TRUE);
		$now = date('Y-m-d H:i:s');
		$data = array('username' => $username,
				'password' => $crypt_pass,
				'email' => $email,
				'created_on' => $now);

		if ($this->db->insert('author',$data)) {
			#$author_id = $this->db->insert_id();
			return $this->db->insert_id();
		} else {
			return false;
		}
	}

	public function author_logout() {
		//clear session variables, set hash to null
		$this->session->sess_destroy();
	}

	public function check_valid_login() {

		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);
		$this->db->where('username',$username);
		$query = $this->db->get('author');
		if ($query->num_rows() == 0) {
			return false;
		} 
		$user = $query->row_array();
		$crypt_pass = crypt($password,$user['password']);
		if ($user['password'] != $crypt_pass) {
			return false;
		}
		//valid login information

		$key = $this->create_key(30);
		$now = date('Y-m-d H:i:s');
		$data = array('hash' => $key, 'last_login' => $now);
		$this->db->where('author_id',$user['author_id']);
		$this->db->update('author',$data);

		$data = array('author_id' => $user['author_id'],
			      'hash' => $key);
		$this->session->set_userdata($data);
		return $user['username'];

	}

	private function create_key($len){
		$str = "";
		for($i=1;$i<=$len;$i++)$str.=base_convert(rand(0,35),10,36);
		return $str;
	}

	public function get_change_password_rules() {
		$rules['old_password'] = "required|min_length[6]|alpha_dash";
		$rules['new_password'] = "required|min_length[6]|alpha_dash";
		$rules['confirm_password'] = "matches[new_password]|min_length[6]|alpha_dash";
		return $rules;
	}

	public function get_change_password_fields() {
		$fields['old_password'] = "Old Password";
		$fields['new_password'] = "New Password";
		$fields['confirm_password'] = "Confirmation Password";
		return $fields;
	}

	public function change_password($user) {

		$old = $this->input->post('old_password',TRUE);
		$new = $this->input->post('new_password',TRUE);
		$confirm = $this->input->post('confirm_password',TRUE);
		$crypt_pass = crypt($old,$user['password']);
		if ($crypt_pass != $user['password']) {
			return false;
		}

		$new_crypt_pass = crypt($new);
		$this->db->where('username',$user['username']);
		$data = array('password' => $new_crypt_pass);
		$this->db->update('author',$data);

		return true;

	}

}

?>
